#include <QtGui/QApplication>
#include <QSqlDatabase>
#include <QSettings>
#include <QMessageBox>
#include "mainwindow.h"
#include "globals.h"
#include <settings.h>

QString dbDriver;
QString dbHost;
unsigned int dbPort;
QString dbName;
QString dbUser;
QString dbPassword;
QSqlDatabase dBHandler;
bool isSettingsCorrect;

QString tmpDir;

//int loadSettings();

int main(int argc, char *argv[])
{
    bool fictive = true;
    QApplication app();
    //
    // Setting application information
    //

    QCoreApplication::setApplicationName("WhiteKnight");
    QCoreApplication::setApplicationVersion("0.1b");
    QCoreApplication::setOrganizationName("SSC");
    QCoreApplication::setOrganizationDomain("http://ed-sar.livejournal.com");
    QSettings setCheck(QCoreApplication::organizationName(),
                       QCoreApplication::applicationName());

    QApplication a(argc, argv);

    if ( setCheck.contains("database/driver") &&
         setCheck.contains("database/host") &&
         setCheck.contains("database/port") &&
         setCheck.contains("database/user") &&
         setCheck.contains("database/name") &&
         setCheck.contains("database/password"))
        isSettingsCorrect = true;
    else
        isSettingsCorrect = false;

    //
    // Restoring application's settings
    //

    //tmpDir = setCheck.value("temporary", QCoreApplication::applicationDirPath()).toString();

    dbDriver = setCheck.value("database/driver").toString();
    dbHost = setCheck.value("database/host", "localhost").toString();
    dbPort = setCheck.value("database/port", 5300).toInt();
    dbUser = setCheck.value("database/user", "root").toString();
    dbName = setCheck.value("database/name", "white_knight").toString();
    dbPassword = setCheck.value("database/password", "azazello").toString();



    dBHandler = QSqlDatabase::addDatabase("QMYSQL");
    dBHandler.setHostName(dbHost);
    dBHandler.setDatabaseName(dbName);
    dBHandler.setUserName(dbUser);
    dBHandler.setPassword(dbPassword);

    fictive = dBHandler.open();

    if(fictive)
        qDebug("connected!");
    else
    {

    }
    MainWindow mW;
    mW.show();
    return a.exec();
}
